Flatlist
FlatList is a versatile list component with several useful features, including headers, footers, and scrolling capabilities.
In the next example, we create a simple todo app that looks the following:

Using the FlatList component:
- Import the
FlatListcomponent from React Native:
import {
StyleSheet,
Text,
View,
Button,
TextInput,
FlatList,
} from "react-native";
- Declare new states to manage text input value and FlatList items:
// import useState
import { useState } from 'react';
// declare states
const [todo, setTodo] = useState("");
const [todos, setTodos] = useState([]);
- Next, we render the
TextInputandButtoncomponents:
return (
<View style={styles.container}>
<TextInput
value={todo}
onChangeText={text => setTodo(text)}
placeholder='Enter a new task...'
/>
<Button title='Add' onPress={handlePress} />
</View>
);
- The
handlePressfunction is called when the Add button is pressed. This function adds the current value of thetodostate (representing the new task) to thetodosstate array. After adding the task to the array, thetodostate is reset to an empty string, clearing theTextInputfor the next task input.
const handlePress = () => {
setTodos([...todos, { key: todo }]);
setTodo("");
};
- Finally, we render the
FlatListcomponent to display all todos.
<FlatList
data={todos}
renderItem={({item}) => <Text>{item.key}</Text>}
/>
- The
renderItemprop defines how data items are rendered within the list. Thedataprop indicates the source of the data, which must be an array. ThekeyExtractorprop extracts a unique key for each item in the list. It that is not defined, index will be used.
Task
Read the FlatList documentation in https://reactnative.dev/docs/flatlist.
- Add
ListEmptyComponentto show text "No data" when the list is empty. - Use
ItemSeparatorComponentto render thin line between each list item. You can create aViewwith a height of 1 and a background color to render a line separator between items in aFlatList.
There is also FlashList component (https://shopify.github.io/flash-list/) that is developed by Shopify. It is designed to handle large datasets efficiently. It is optimized for memory usage and it is suitable for applications that require smooth scrolling and rendering of large lists.